﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace ORMTool
{
    public class ValidationInfoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.ToString().Trim().Length > 0)
            {
                return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
    public class CharToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.ToString().Trim().Length > 0)
            {
                if (value.ToString().Trim() == "1")
                    return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                bool isTry = false;
                if (bool.TryParse(value.ToString().Trim(), out isTry))
                {
                    if (isTry)
                        return "1";
                }
            }
            return "0";
        }
    }
    public class StringToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                bool isTry = false;
                if (bool.TryParse(value.ToString().Trim(), out isTry))
                    return isTry;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                bool isTry = false;
                if (bool.TryParse(value.ToString().Trim(), out isTry))
                {
                    if (isTry)
                        return "true";
                }
            }
            return "false";
        }
    }
    //public class BY01Validation : ValidationRule
    //{
    //    public override System.Windows.Controls.ValidationResult Validate(object value, CultureInfo cultureInfo)
    //    {
    //        var bindingGroup = value as BindingGroup;
    //        var album = bindingGroup?.Items.OfType<T_XSXX>().ElementAtOrDefault(0);
    //        if (album != null)
    //        {
    //            ValidationContext context = new ValidationContext(album, null, null);
    //            List<ValidationResult> validationResults = new List<ValidationResult>();
    //            bool valid = Validator.TryValidateObject(album, context, validationResults, true);
    //            string error = string.Join(";", validationResults.Select(p => p.ErrorMessage).ToArray());
    //            album.BY01 = error;
    //            return new System.Windows.Controls.ValidationResult(valid, $"校验信息：{album.BY01.Trim()};");
    //        }
    //        return System.Windows.Controls.ValidationResult.ValidResult;
    //    }
    //}

}
